<template>
  <div>
    <el-card>
      <el-page-header icon="" title="成绩管理">
        <template #content>
          <span class="text-large font-600 mr-3">成绩分析</span>
        </template>
      </el-page-header>

      <el-table :data="tableData" style="width: 100%" stripe v-loading="loading">
        <el-table-column prop="courseid" label="课程编号" />
        <el-table-column prop="coursename" label="课程名称" />
        <el-table-column prop="major" label="专业" />
        <el-table-column prop="term" label="学期">
          <template #default="scope">
            {{ scope.row.term === 20231 ? '2023-2024学年第1学期' : '2023-2024学年第2学期' }}
          </template>
        </el-table-column>
        <el-table-column prop="credit" label="学分" />
        <el-table-column prop="name" label="任课教师" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="default" @click="handleAnalysis(scope.row)" type="primary">查看分析</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import axios from 'axios'
const tableData = ref([])
const loading = ref(true)
const store = useStore()
const router = useRouter()
onMounted(() => {
  getTableData()
  // console.log(tableData)
})

const getTableData = async () => {
  if (store.state.userInfo.role === 1) {
    const res1 = await axios.get('/api/course/listpublished')
    const res2 = await axios.get('/api/user/getteacherlist')
    // 将教师工号改为名字
    res1.data.data.forEach((item) => {
      res2.data.data.forEach((i) => {
        if (item['courseteacher'] === i['username']) {
          item['name'] = i['name']
        }
      })
    })
    tableData.value = res1.data.data
  } else if (store.state.userInfo.role === 2) {
    const res1 = await axios.get(`/api/course/mylistpublished/${store.state.userInfo.username}`)
    const res2 = await axios.get('/api/user/getteacherlist')
    // 将教师工号改为名字
    res1.data.data.forEach((item) => {
      res2.data.data.forEach((i) => {
        if (item['courseteacher'] === i['username']) {
          item['name'] = i['name']
        }
      })
    })
    tableData.value = res1.data.data
  }
  loading.value = false
}
const handleAnalysis = (item) => {
  router.push(`/score-manage/scoreanalysis/${item._id}`)
}
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 50px;
}
</style>
